package com.dk.controller;

import com.dk.api.CodePartServiceAPI;
import com.dk.api.CodeRuleServiceAPI;
import com.dk.fectory.CodeFectory;
import com.dk.pojo.CodePart;
import com.dk.pojo.CodeRule;
import com.dk.result.Result;
import com.dk.result.ResultConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

/**
 * @author LTZ
 * @create 2023/5/10
 */
@RestController
@RequestMapping("getCode")
@RefreshScope
@Api(tags = "获取编码接口")
public class GetCodeController {

    @Autowired
    private CodeRuleServiceAPI codeRuleServiceAPI;
    @Autowired
    private CodePartServiceAPI codePartServiceAPI;

    @GetMapping("getCodeMessage")
    @ApiOperation("获取编码")
    @ApiImplicitParam(name = "code", value = "编码规则",required = true)
    public Result getCode(String code){
        String ruleCode = code;
        //远程调用,根据编码查询当前对象集合
        List<CodeRule> ruleList = codeRuleServiceAPI.selectByRuleCode(ruleCode);
        if(ruleList.size() ==  0){
            return new Result(ResultConstant.GET_CODE_NULL,ResultConstant.GET_CODE_MESSAGE);
        }
        List<CodePart> partList = codePartServiceAPI.selectPartByRuleId(ruleList.get(0).getRuleId());
        if (partList.size() < 2) {
            return new Result(ResultConstant.GET_CODE_SIZE_LESS_TWO,ResultConstant.GET_CODE_SIZE_MESSAGE);
        }
        String codeStr = "";
        for (CodePart codePart : partList) {
            //使用策略设计模式
            codeStr += CodeFectory.map.get(codePart.getPartType()).code(codePart);
            //如果是流水号
            if(codePart.getPartType().equals("SERIALNO")){
                if(codePart.getSeriaNowNo() == null || codePart.getSeriaNowNo().equals("")){
                    //流水号现在的值+流水号步长
                    codePart.setSeriaNowNo(codePart.getSeriaStartNo() + codePart.getSeriaStep());
                    codePartServiceAPI.updateCodePartAPI(codePart);
                    //流水号是循环
                    if (codePart.getCycleFlag().equals("Y")) {
                        //是循环,设置修改时间
                        codePart.setUpdateTime(new Date());
                        codePartServiceAPI.updateCodePartAPI(codePart);
                    }
                }else {
                    //流水号现在的值+流水号步长
                    codePart.setSeriaNowNo(codePart.getSeriaNowNo() + codePart.getSeriaStep());
                    codePartServiceAPI.updateCodePartAPI(codePart);
                    //是否循环
                    if (codePart.getCycleFlag()!=null && codePart.getCycleFlag().equals("Y")){
                        //最后一次修改的时间-开始创建的时间
                        long l = codePart.getUpdateTime().getTime() - codePart.getCreateTime().getTime();
                        //按年循环
                        if (codePart.getCycleMethod().equals("YEAR")){
                            int a = (int)l/(1000*3600*24);
                            if (a>=366){
                                //大于一年
                                codePart.setCreateTime(new Date());
                                codePart.setUpdateTime(new Date());
                                //把流水号值回归初始
                                codePart.setSeriaNowNo(codePart.getSeriaStartNo());
                                codePartServiceAPI.updateCodePartAPI(codePart);
                            }else {
                                codePart.setUpdateTime(new Date());
                                codePartServiceAPI.updateCodePartAPI(codePart);
                            }
                            //按月循环
                        }else if(codePart.getCycleMethod().equals("MONTH")){
                            int a = (int)l/(1000*3600*24);
                            //大于一月
                            if (a>=31){
                                codePart.setCreateTime(new Date());
                                codePart.setUpdateTime(new Date());
                                //把流水号值回归初始
                                codePart.setSeriaNowNo(codePart.getSeriaStartNo());
                                codePartServiceAPI.updateCodePartAPI(codePart);
                            }else {
                                codePart.setUpdateTime(new Date());
                                codePartServiceAPI.updateCodePartAPI(codePart);
                            }
                            //按天循环
                        }else if (codePart.getCycleMethod().equals("DAY")){
                            int a = (int)l/(1000*3600);
                            //大于一天
                            if (a>=24){
                                codePart.setCreateTime(new Date());
                                codePart.setUpdateTime(new Date());
                                //把流水号值回归初始
                                codePart.setSeriaNowNo(codePart.getSeriaStartNo());
                                codePartServiceAPI.updateCodePartAPI(codePart);
                            }else {
                                codePart.setUpdateTime(new Date());
                                codePartServiceAPI.updateCodePartAPI(codePart);
                            }
                            //按小时循环
                        }else if (codePart.getCycleMethod().equals("HOUR")) {
                            int a = (int) l / (1000 * 60);
                            //大于一小时
                            if (a >= 60) {
                                codePart.setCreateTime(new Date());
                                codePart.setUpdateTime(new Date());
                                //把流水号值回归初始
                                codePart.setSeriaNowNo(codePart.getSeriaStartNo());
                                codePartServiceAPI.updateCodePartAPI(codePart);
                            } else {
                                codePart.setUpdateTime(new Date());
                                codePartServiceAPI.updateCodePartAPI(codePart);
                            }
                            //按分钟循环
                        }else if (codePart.getCycleMethod().equals("MINITE")){
                            int a = (int)l/(1000);
                            //大于一分钟
                            if (a>=60){
                                codePart.setCreateTime(new Date());
                                codePart.setUpdateTime(new Date());
                                //把流水号值回归初始
                                codePart.setSeriaNowNo(codePart.getSeriaStartNo());
                                codePartServiceAPI.updateCodePartAPI(codePart);
                            }else {
                                codePart.setUpdateTime(new Date());
                                codePartServiceAPI.updateCodePartAPI(codePart);
                            }
                        }
                    }
                }
            }
        }
        return new Result(ResultConstant.CODE,ResultConstant.MESSAGE,codeStr);
    }

}